/*
 * BanBu_VideoViewCell
 * @description 本文件提供聊天界面对话界面显示视频消息的cell
 * @package
 * @author 		***
 * @copyright
 * @version 		1.0
 * @description 本文件提供聊天界面对话界面显示视频消息的cell
 */

#import "BanBu_VideoViewCell.h"
#import "BanBuAPIs.h"
#import "AppDataManager.h"
#import "AppCommunicationManager.h"

@implementation BanBu_VideoViewCell
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        //视频
        _videoView  = [[BanBu_VideoView alloc]initWithFrame:CGRectZero];
        [self addSubview:_videoView];
        
        _activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        [_activityIndicator setFrame:CGRectMake(0, 17, 30, 30)];
        _activityIndicator.center = self.bkView.center;
        [_activityIndicator setHidesWhenStopped:YES];
        [_activityIndicator setColor:[UIColor blackColor]];
        
        [_activityIndicator  startAnimating];
        [self addSubview:_activityIndicator];
        
        //阅后即焚消息倒计时背景
        _backgroundTime = [[UIImageView alloc]initWithFrame:CGRectZero];
        _backgroundTime.backgroundColor = [UIColor clearColor];
        _backgroundTime.image = [UIImage imageNamed:@"sexangle_black.png"];
        _backgroundTime.hidden = YES;
        [self addSubview:_backgroundTime];
        
        _snapTimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.bkView.frame.origin.x+5, self.bkView.frame.origin.y-2, 20, 20)] ;
        _snapTimeLabel.font = [UIFont systemFontOfSize:12];
        _snapTimeLabel.textColor = [UIColor whiteColor];
        _snapTimeLabel.backgroundColor = [UIColor clearColor];
        _snapTimeLabel.textAlignment = UITextAlignmentCenter;
        _snapTimeLabel.hidden = YES;
        [self addSubview:_snapTimeLabel];
        
    }
    return self;
}

-(void)dealloc
{
    [_activityIndicator release],_activityIndicator = nil;
    [_videoView release],_videoView = nil;
    
    [_backgroundTime release],_backgroundTime = nil;
    [_snapTimeLabel release],_snapTimeLabel = nil;
    [super dealloc];
}

-(void)layoutSubviews
{
    self.bkViewWidth = 160.0;
    [super layoutSubviews];
    
    self.bkView.alpha = 0;
    _videoView.frame = MediaViewRect00;
    
    if (self.atLeft)
    {
        _videoView.frame = CGRectMake(_videoView.frame.origin.x-10, _videoView.frame.origin.y,_videoView.frame.size.width,_videoView.frame.size.height);
    }
    else
    {
        _videoView.frame = CGRectMake(_videoView.frame.origin.x+15, _videoView.frame.origin.y,_videoView.frame.size.width,_videoView.frame.size.height);
//        self.statusView.frame = CGRectMake(self.statusView.frame.origin.x25, self.statusView.frame.origin.y, self.statusView.frame.size.width, self.statusView.frame.size.height);
    }
    
    self.activityIndicator.center = _videoView.center;
    
    if (_videoView.snapMsg)
    {
        [self setShowFromFrame:ChatMsgVideo];
        CGRect frame = self.bkView.frame;
        
        if (self.atLeft)
        {
            _snapTimeLabel.frame = CGRectMake(frame.origin.x+frame.size.width+5, frame.origin.y-2, 20, 20);
            _backgroundTime.frame = CGRectMake(frame.origin.x+frame.size.width+5, frame.origin.y-2, 20, 20);
        }
        else
        {
            _snapTimeLabel.frame = CGRectMake(frame.origin.x+5, frame.origin.y-2, 20, 20);
        }
    }
    self.avatar.frame = AvatarRect01;
//    [self.vipImageView setFrame:CGRectMake(self.avatar.frame.origin.x + 28, self.avatar.frame.origin.y + 26, 15, 10)];
}

-(void)setMsgMediastatus:(MediaStatus)status
{
    if(status != MediaStatusNormal && status != MediaStatusReaded)//不是：已播过或正常时，loading转圈
    {
        [self.activityIndicator  startAnimating];
    }
    else
    {
        [self.activityIndicator  stopAnimating];
    }
}

//设置cell的各种信息
-(void)setChatCellInfo:(NSDictionary *)amsg
{
    self.bkViewWidth = 90.0;
    [super setChatCellInfo:amsg];
    
    _videoView.snapMsg = [VALUE(KeyShowFrom, amsg) isEqualToString:@"snap"];
    self.showFrom = _videoView.snapMsg;
    _effectiveTime = [[amsg valueForKey:KeyEffectiveTime] integerValue];
    
    MediaStatus status = [VALUE(KeyMediaStatus, amsg) integerValue];
    
    [_videoView setVideo:[amsg valueForKey:KeyContent]];
    [_videoView setThumbnail:[amsg valueForKey:KeyThumbnail]];
    [_videoView setStatus:status];
    
    [self setMsgMediastatus:status];
    
    if(status == MediaStatusDownload)
    {
        [AppComManager getBanBuMedia:[amsg valueForKey:KeyContent] forMsgID:VALUE(KeyChatid, amsg) fromUid:MyAppDataManager.chatuid delegate:MyAppDataManager];
        [AppComManager getBanBuMediaThumbnail:[amsg valueForKey:KeyThumbnail] forMsgID:VALUE(KeyChatid, amsg) fromUid:MyAppDataManager.chatuid delegate:MyAppDataManager];
    }
}

@end
